(function(root) {
  var _doc = document,
      _$ = function (name){
        return _doc.querySelector(name);
      },
      boxName = 'XUI-modal',
      maskName = 'modal-mask',
      //默认配置
      config = {
    		mask: true,
        maskClose: true,
        animateClass: 'modal-animate',
      };
  Modal.fn = Modal.prototype;
  //Modal
  function Modal (parameter) {
    var obj = {};
    if(parameter[0] instanceof Array === false && typeof parameter[0] === 'object'){
      obj = parameter[0];
      obj.content = 'XUIModal提示:弹窗未传入提示内容';
    }else{
      obj = parameter[1] || {};
      obj.content = parameter[0];
    };
    //参数继承
    this.config = this.extend(obj);
    this.html();
  };
  //配置拷贝
  Modal.fn.extend = function (obj) {
    //不改变原始config
    var _config = JSON.parse(JSON.stringify(config));
    for(var i in obj){
      _config[i] = obj[i];
    };
    return _config;
  };
  //生成html
  Modal.fn.html = function () {
    this.box = _doc.createElement('div');
    var self = this,
        btnEl = '',
        title = '',
        _class = '',
        content = '',
        mask = '';
    this.box.id = this.box.className = boxName;
    //判断是否添加遮罩
    self.config.mask && (mask = '<div class="modal-mask"></div>');
    //判断按钮是否存在
    if(self.config.btn && self.config.type != 'msg'){
      btnEl = '<div class="modal-btn-box">'+
                (self.config.btn[0] ? '<span>'+(self.config.btn[0])+'</span>' : '')+
                (self.config.btn[1] && self.config.type !== 'footer' ? '<span>'+(self.config.btn[1])+'</span>' : '')+
              '</div>';
    };
    if(self.config.type === undefined || self.config.type === 'msg'){
      content = '<div class="modal-content">'+self.config.content+'</div>'
    };
    self.config.type !== 'msg' && (title = (self.config.title ? '<div class="modal-title">'+self.config.title+'</div>' : ''));
    _class = 'XUI-modal-'+self.config.type;
    //类型判断
    switch(self.config.type){
      case 'msg':
        title = '';
        break;
      case 'footer':
        for(var i in self.config.content){
          content += '<div class="modal-content">'+self.config.content[i]+'</div>';
        };
        break;
      default :
        title && (_class += ' show-title');
        break;
    };
    self.box.innerHTML = mask+//遮罩
                    '<div class="modal-box">'+
                      '<div class="modal '+(self.config.animateClass)+' '+ _class+'">'+
                        title+
                        content+
                        btnEl+//按钮
                      '</div>'+
                    '</div>';
    //将dom添加进body
    _doc.body.appendChild(this.box);
    self.event();
  };
  //绑定事件
  Modal.fn.event = function () {
    var self = this,
        time = self.config.time;
    //定时器
    if(time){
      self.time = setTimeout(function () {
        self.Close();
      } , time);
    };
    //遮罩点击关闭
    self.config.maskClose && self.bindEvent(self.box.querySelector('.'+maskName) , hiden);
    if(self.config.type !== 'msg' && self.config.btn){
      //取消按钮
      self.config.btn[0] && self.bindEvent(self.box.querySelectorAll('.modal-btn-box span')[0] , hiden);
      //确定按钮
      self.config.btn[1] && self.bindEvent(self.box.querySelectorAll('.modal-btn-box span')[1] , function () {
        self.Close();
        self.config.yesBack && self.config.yesBack();
      });
    };
    //列表按钮绑定事件
    if(self.config.type === 'footer'){
      var list = self.box.querySelectorAll('.modal-content');
      for(var i in self.config.footerBakc){
        list[i].index = i;
        self.bindEvent(list[i] , function () {
          self.Close();
          self.config.footerBakc[this.index]();
        });
      };
    };
    //取消
    function hiden() {
      self.Close();
      self.config.noBack && self.config.noBack();
    };
  };
  //绑定事件
  Modal.fn.bindEvent = function (el , callback) {
    el.addEventListener('click' , function (e) {
      callback.call(this , e);
    } , false);
  };
  //关闭
  Modal.fn.Close = function () {
    var self = this;
    _doc.body.removeChild(self.box);
    clearTimeout(self.time);
  };
  //XUIModal
  var XUIModal = {
    alert (content , json) {
      new Modal(arguments);
    },
    msg (content , json) {
      if(json === undefined){
        arguments[1] = {};
      };
      arguments[1].type = 'msg';
      new Modal(arguments);
    },
    footer (content , json) {
      if(json === undefined){
        arguments[1] = {};
      };
      arguments[1].type = 'footer';
      arguments[1].btn = ['取消'];
      //animateClass
      arguments[1].animateClass = arguments[1].animateClass || 'XUI-modal-upward2';
      new Modal(arguments);
    },
    v () {
      console.log('V1.0');
    }
  };
  //检测是否是模块化引入
  if(typeof define === 'function'){
    define(function (a , b , c) {
      return XUIModal;
    });
  }else{
    //自动引入css
    var scripts = document.scripts,
        path = scripts[scripts.length - 1].src,
        link = document.createElement('link');
    link.href = path.substring(0 , path.lastIndexOf('/')+1) + 'css/XUI-modal-mobile.css';
    link.type = 'text/css';
    link.rel = 'styleSheet';
    document.head.appendChild(link);
    //接口
    window.XUIModal = XUIModal;
  };
})(window);
